package com.blt.car.home;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blt.car.R;
import com.blt.car.base.BaseTitleBarActivity;
import com.blt.car.base.ViewHolder;
import com.bumptech.glide.Glide;

import blt.app_core.api.base.CoreManager;
import blt.app_core.api.car.CarApi;
import blt.app_core.api.car.bean.Car;
import blt.app_core.utils.CommonPref;
import blt.app_core.utils.PreferencesConstant;
import blt.app_core.utils.UriProdiver;

/**
 * Created by xgp on 2017/6/23.
 */
public class CarDetailActivity extends BaseTitleBarActivity {
    private Car car;

    private ImageView imCover;
    private TextView carModelNumber;
    private TextView carType;
    private TextView carSeats;
    private TextView carNumber;
    private Button choseCar;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_car_detail;
    }

    @Override
    protected void initData() {
        super.initData();
        if (getIntent().getExtras() != null) {
            car = (Car) getIntent().getExtras().getSerializable(CarListActivity.CAR_DETAIL);
        }
    }


    @Override
    protected void initView(ViewHolder holder) {
        mTitleBar.setTitle("车辆详情");
        imCover = holder.get(R.id.car_detail_cover);
        carNumber = holder.get(R.id.car_detail_car_no);
        carModelNumber = holder.get(R.id.car_detail_car_type);
        carType = holder.get(R.id.car_detail_model);
        carSeats = holder.get(R.id.car_detail_seats);
        choseCar = holder.get(R.id.car_detail_btn_add);
        holder.get(R.id.car_detail_look_paper);

        if (car != null) {
            Glide.with(this).load(UriProdiver.IMAGE_BASE_URL + car.carPicFront).into(imCover);
            carNumber.setText(car.carPlateNumber);
            carModelNumber.setText(car.carModel);
            if (car.carType == 0) {
                carType.setText("小型客车");
            } else {
                carType.setText("大型客车");
            }
            carSeats.setText(car.carSeat + "座");

        }

        holder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.car_detail_btn_add) {
                    if (car.carStatus == 0) {
                        toastShort("待审核的车辆不能选择");
                        return;
                    }
                    toastShort("选择成功");
                    CommonPref.getInstance().saveInt(PreferencesConstant.CAR_ID, car.carId);
                    CoreManager.getCore(CarApi.class).setDefaultCar(car.carId);
                    CoreManager.notifyClients(CarApi.class, CarApi.onUpdataHomeCar, car);
                    finish();
                } else if (v.getId() == R.id.car_detail_look_paper) {
                    openActivity(PictureLookActivity.class, PictureLookActivity.IMAGE_URI, car.carPicLicense);
                }

            }
        }, R.id.car_detail_btn_add, R.id.car_detail_look_paper);

    }
}
